package com.incar.chuxing.utils.wangyiqiyu;

import android.content.Context;
import android.text.TextUtils;

import com.incar.chuxing.api.ApiStore;
import com.incar.chuxing.api.UrlContainer;
import com.incar.chuxing.application.MyApplication;
import com.incar.chuxing.bean.QualificationBean;
import com.incar.chuxing.utils.SPKey;
import com.incar.chuxing.utils.SPUtils;
import com.qiyukf.unicorn.api.ConsultSource;
import com.qiyukf.unicorn.api.ProductDetail;
import com.qiyukf.unicorn.api.Unicorn;
import com.qiyukf.unicorn.api.YSFUserInfo;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by 17310 on 2018/3/30.
 */

public class ConsultService {
    private Context mContext;

    public ConsultService(Context mContext) {
        this.mContext = mContext;
    }

    public void consultService(final Context context, String uri, String title, String custom, ProductDetail productDetail) {
        // 启动聊天界面
        setUserInfo();
        ConsultSource source = new ConsultSource(uri, title, custom);
        source.productDetail = productDetail;
        Unicorn.openServiceActivity(context, staffName(), source);

    }

    private String staffName() {
        return "大道用车客服";
    }

    private void setUserInfo() {
        ApiStore.getInstance().getApiService(UrlContainer.Request.BASE_GATE_URL).getUserStatus(MyApplication.getInstance().getRequestFieldMap()).enqueue(new Callback<QualificationBean>() {
            @Override
            public void onResponse(Call<QualificationBean> call, Response<QualificationBean> response) {
                if (response.isSuccessful() && response.body() != null) {
                    if (response.body().getStatus() == 200) {
                        SPUtils.getInstance(mContext).putStringValue(SPKey.REAL_NAME_STATUS, response.body().getAttachment().getIdentityStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.LICENSE_CAR_STATUS, response.body().getAttachment().getLicenseStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.DEPOSIT_CAR_STATUS, response.body().getAttachment().getDepositStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.DEPOSIT_AMOUNT, response.body().getAttachment().getCarBail() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.JISHEN_STATUS, response.body().getAttachment().getStatus() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.ZHIMA_STATUS, response.body().getAttachment().getAuthorize() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.ZHIMA_FEN, response.body().getAttachment().getScore() + "");
                        SPUtils.getInstance(mContext).putStringValue(SPKey.XINYONG_LEVEL, response.body().getAttachment().getLevel() + "");

                        String licenseStatus = response.body().getAttachment().getLicenseStatus() + "";
                        String depositStatus = response.body().getAttachment().getDepositStatus() + "";
                        String depositAmount = response.body().getAttachment().getCarBail() + "";
                        String jishenStatus = response.body().getAttachment().getStatus() + "";
                        String score = response.body().getAttachment().getScore() + "";
                        String level = response.body().getAttachment().getLevel();
                        String authorize = response.body().getAttachment().getAuthorize() + "";
                        //13 已认证
                        String shenfen = "";
                        if ("13".equals(licenseStatus) || "18".equals(jishenStatus)) {
                            shenfen = "已认证";
                        } else if ("10".equals(licenseStatus) && !"18".equals(jishenStatus)) {
                            shenfen = "未认证";
                        } else if ("11".equals(licenseStatus)) {
                            shenfen = "审核中";
                        } else if ("12".equals(licenseStatus)) {
                            shenfen = "未认证";
                        }
                        String xinyong = "";
                        if ("1".equals(depositStatus) || "1".equals(authorize)) {
                            xinyong = level;
                        } else if ("0".equals(depositStatus) && !"1".equals(authorize)) {
                            xinyong = "未认证";
                        } else {
                            xinyong = "已授权未通过";
                        }
                        String name = response.body().getAttachment().getUserName();
                        String phone = SPUtils.getInstance(mContext).getStringValue(SPKey.USER_PHONE_NUM);
                        YSFUserInfo userInfo = new YSFUserInfo();
                        // APP 的用户 ID
                        userInfo.userId =MyApplication.getInstance().getUid();
                        // 当且仅当开发者在管理后台开启了 authToken 校验功能时，该字段才有效
                        userInfo.authToken =MyApplication.getInstance().getToken();
                        // CRM 扩展字段
                        userInfo.data = userInfoData(name, phone, "", shenfen, xinyong).toString();
                        Unicorn.setUserInfo(userInfo);
                    } else {
                    }
                } else {
                }
            }

            @Override
            public void onFailure(Call<QualificationBean> call, Throwable t) {
            }
        });
    }

    private JSONArray userInfoData(String name, String mobile, String email, String auth, String card) {
        JSONArray array = new JSONArray();
        array.put(userInfoDataItem("real_name", name, false, -1, null, null)); // name
        array.put(userInfoDataItem("mobile_phone", mobile, false, -1, null, null)); // mobile
        array.put(userInfoDataItem("email", email, true, -1, null, null)); // email
        array.put(userInfoDataItem("real_name_auth", auth, false, 0, "身份认证", null));
        array.put(userInfoDataItem("bound_bank_card", card, false, 1, "信用认证", null));
        return array;
    }

    private JSONObject userInfoDataItem(String key, Object value, boolean hidden, int index, String label, String href) {
        JSONObject item = new JSONObject();
        try {
            item.put("key", key);
            item.put("value", value);
            if (hidden) {
                item.put("hidden", true);
            }
            if (index >= 0) {
                item.put("index", index);
            }
            if (!TextUtils.isEmpty(label)) {
                item.put("label", label);
            }
            if (!TextUtils.isEmpty(href)) {
                item.put("href", href);
            }
            return item;
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return item;
    }
}
